package com.example.yan.excel_split.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.*;
import java.util.stream.Collectors;

public class test1 {

    public static void main(String[] args) {
        ExcelReader fileReader = ExcelUtil.getReader("C:\\Users\\75763\\Desktop\\SHOP.105110000000005.20211013.02.success(1).xls");
        List<List<Object>> readAll = fileReader.read();
        List<List<String>> dcc = new ArrayList<>();
        List<String> abc = new ArrayList<>();
        for (List<Object> object : readAll) {
            List<String> list = object.stream().map(o -> o.toString()).collect(Collectors.toList()); // 先转成 string
            List<String> filtered = list.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); //isEmpty 去掉空的值
            //  List<String> filtereds = filtered.stream().filter(x -> x.equals(regex)).collect(Collectors.toList());
            //  System.out.println("88" + filtereds);
            abc.addAll(filtered);
            String sings = JSON.toJSONString(filtered, SerializerFeature.WriteMapNullValue);

            if (sings.contains("借记卡")) {

                  System.out.println("--"+sings);

                //把所有的中括号取消
                String str = sings.substring(sings.indexOf("[") + 1, sings.lastIndexOf("]"));

                // System.out.println("--"+str);

                //逗号分隔开
                List<String> strList = Arrays.asList(str.split(","));

                System.out.println("--1" + strList);
//                for (int i = 0; i < abc.size(); i++){
//                    System.out.println("===>"+strList.get(i));
//                }
                dcc.add(strList);


            }

            //  System.out.println(s);
            // test1.writer(s);
            //   System.out.println(writer(s));
        }
//        for (User user : users) {
//            System.out.println("輸出user對象"+user.toString());
//        }

//        Pattern p = Pattern.compile(regex);
//
//
//        for (int i = 0; i < abc.size(); i++) {
//            Matcher m = p.matcher(abc.get(Integer.parseInt("0")));writer
//            System.out.println("88"+ m.matches());
//        }
        System.out.println("--------------------------------------------------------------------");
        writer(dcc,abc);
        System.out.println("111"+abc);
        System.out.println("222"+dcc);
    }

    public static void writer(List<List<String>> abc, List<String> s) {
        String ad = JSON.toJSONString(s, SerializerFeature.WriteMapNullValue);
        System.out.println("JSON转义" + ad);
        String s0 = s.get(8);
        String s1 = s.get(9);
        System.out.println(s0 + s1);//商户编号

        //  String[] a = new String[s.length()];

        //    List<String> list = Arrays.asList(a);

        //    System.out.println(list.get(4));

        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("E:\\桌面\\新建文件夹(4)\\888881.xlsx");


//自定义标题别名
//        writer.addHeaderAlias("1111", "终端号");
//        writer.addHeaderAlias("222", "发卡行");
//        writer.addHeaderAlias("55", "卡号-序列号");
//        writer.addHeaderAlias("444", "交易日期");
//        writer.addHeaderAlias("6666", "交易类型");



      //  List<String> row2 = new ArrayList<>();


        List<String> list2 = new ArrayList<>();



        System.out.println("6666"+list2);

//       String ff= "终端号","发卡行","卡种","卡号-序列号","交易日期","交易时间","交易类型","授权号","交易金额","小费",
//           "分期期数","银行手续费","DCC返还手续费","划账金额","凭证号","批次号","POS交易序号","结算账号","订单号"
//            ,"柜台编号","系统参考号","持卡人姓名","付款凭证号","备注1","备注2";

//        List<String> row1 = CollUtil.newArrayList("终端号","发卡行","卡种","卡号-序列号","交易日期","交易时间","交易类型","授权号","交易金额","小费",
//                "分期期数","银行手续费","DCC返还手续费","划账金额","凭证号","批次号","POS交易序号","结算账号","订单号"
//                ,"柜台编号","系统参考号","持卡人姓名","付款凭证号","备注1","备注2");

      // for (String gee : abc) {

          //  System.out.println("geee"+gee);
          //  List<String> parse = Arrays.asList(gee);



            //    System.out.println("88"+parse);
          //  List<String> row4 = CollUtil.newArrayList(parse.get(0), parse.get(1), parse.get(2), parse.get(3));
//            List<List<String>> row3 = CollUtil.newArrayList(abc.get(0),abc.get(1),abc.get(2),abc.get(3),abc.get(4),abc.get(5),
//                    abc.get(6),abc.get(7),abc.get(8),abc.get(9),abc.get(10),abc.get(11),abc.get(12),abc.get(13),abc.get(14),abc.get(15)
//                    ,abc.get(16),abc.get(17),abc.get(18),abc.get(19),abc.get(20),abc.get(21),abc.get(22),abc.get(23),abc.get(24));
            //System.out.println("999"+row4);
          //  row1.addAll(row3);
         //   row2.addAll(row4);
//            List<String> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2");
//            List<String> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3");
//            List<String> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4");

       // }

        Map<String, Object> row2 = new LinkedHashMap<>();

        System.out.println("abc"+abc);




        System.out.println("s2"+abc);

        for (int i=0;i<=abc.size();i++){
        row2.put("终端号", abc.get(1).get(0));
        row2.put("发卡行", abc.get(1).get(1));
        row2.put("卡种", abc.get(1).get(2));
        row2.put("卡号-序列号", abc.get(1).get(3));
        row2.put("交易日期", abc.get(1).get(4));

        }


        ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row2);




//
//        List<List<String>> rows1=new ArrayList<>(row3);
//        List<List<String>> rows = CollUtil.newArrayList(row1);

// 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);

// 合并单元格后的标题行，使用默认标题样式
        writer.merge(3, s0 + s1);
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
// 关闭writer，释放内存
      writer.close();


        System.out.println("文件生成成功为止在= E:\\桌面\\新建文件夹(4)\\888881.xlsx");
    }





//    private static String jsonString(List<List<String>> s){
//
//        char[] temp = s.toCharArray();
//
//        int n = temp.length;
//
//        for(int i =0;i<n;i++){
//
//            if(temp[i]==':'&&temp[i+1]=='"'){
//
//                for(int j =i+2;j<n;j++){
//
//                    if(temp[j]=='"'){
//
//                        if(temp[j+1]!=',' &&  temp[j+1]!='}'){
//
//                            temp[j]='”';
//
//                        }else if(temp[j+1]==',' ||  temp[j+1]=='}'){
//
//                            break ;
//
//                        }
//
//                    }
//
//                }
//
//            }
//
//        }
//
//        return new String(temp);
//
//    }








}
